/*NOTICE

This software was produced for the U. S. Government
under Contract No. FA8721-09-C-0002, and is
subject to the Rights in Noncommercial Computer Software
and Noncommercial Computer Software Documentation Clause
(DFARS) 252.227-7014 (JUN 1995)

(c) 2009 The MITRE Corporation. All Rights Reserved.
*/
package org.mitre.xmltransform;

import java.util.*;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;

public class XPathNamespaceContextImpl implements NamespaceContext {

	public XPathNamespaceContextImpl() {
		mMapping = new HashMap<String, String>();
	}
	
	public void addPrefix(String pPrefix, String pNamespaceURI) {
		mMapping.put(pPrefix, pNamespaceURI);
	}
	
	public void setDefaultNamespace(String pDefaultNamespace) {
		mDefaultNamespace = pDefaultNamespace;
	}
	
	public String getNamespaceURI(String prefix) {
		if (prefix == null) {
			throw new IllegalArgumentException("null prefix specified.");
		} else if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
			return mDefaultNamespace;
		} else if (prefix.equals(XMLConstants.XML_NS_PREFIX)) {
			return XMLConstants.XML_NS_URI;
		} else if (prefix.equals(XMLConstants.XMLNS_ATTRIBUTE )) {
			return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
		} else if (mMapping.containsKey(prefix)) {
			return (String)mMapping.get(prefix);
		} else {
			return XMLConstants.NULL_NS_URI;
		}
	}

	public String getPrefix(String namespaceURI) {
		if (namespaceURI == null) {
			throw new IllegalArgumentException("null namespace URI");
		} else if (namespaceURI.equals(mDefaultNamespace)) {
			return XMLConstants.DEFAULT_NS_PREFIX;
		} else if (namespaceURI.equals(XMLConstants.XML_NS_URI)) {
			return XMLConstants.XML_NS_PREFIX;
		} else if (namespaceURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
			return XMLConstants.XMLNS_ATTRIBUTE;
		} else if (mMapping.containsValue(namespaceURI)) {
			String curNS;
			for (Object prefix : mMapping.keySet()) {
				curNS = (String)mMapping.get(prefix);
				if (curNS.equals(namespaceURI)) return (String)prefix;
			}
		}
		return null;
	}

	public Iterator<String> getPrefixes(String namespaceURI) {
		ArrayList<String> result = new ArrayList<String>();
		if (namespaceURI == null) {
			throw new IllegalArgumentException("null namespace URI");
		} else if (namespaceURI.equals(mDefaultNamespace)) {
			result.add(XMLConstants.DEFAULT_NS_PREFIX);
			return result.iterator();
		} else if (namespaceURI.equals(XMLConstants.XML_NS_URI)) {
			result.add(XMLConstants.XML_NS_PREFIX);
			return result.iterator();
		} else if (namespaceURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
			result.add(XMLConstants.XMLNS_ATTRIBUTE);
			return result.iterator();
		} else {
			String curNS;
			for (Object prefix : mMapping.keySet()) {
				curNS = (String)mMapping.get(prefix);
				if (curNS.equals(namespaceURI)) {
					result.add((String)prefix);
				}
			}
			return result.iterator();
		}
	}

	private String mDefaultNamespace = XMLConstants.NULL_NS_URI;
	private Map<String, String> mMapping;
}
